package proj1.util;

import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;

/**
 * <pre>
 * Simple logger class for standardizing output formats.
 * </pre>
 * @author Jason Campos
 */
public class Logger {
	private String className;
	private static Map<Class<?>,Logger> instances = new HashMap<Class<?>,Logger>();
	/**
	 * <pre>
	 * Retrieves the logger instance for the specified class.
	 * </pre>
	 * @param logForClass
	 * @return
	 */
	public static Logger getLogger(Class<?> logForClass){
		if(!instances.containsKey(logForClass))	
			instances.put(logForClass, new Logger(logForClass));
		return instances.get(logForClass);
	}
	
	public void info (String msg){
		log(System.out, "INFO - " + msg);
	}
	
	public void warn (String msg){
		log(System.out, "WARN - " + msg);
	}
	
	public void error(String msg){
		log(System.err, msg);
	}

	public void error(String msg, Exception e){
		log(System.err, "ERROR - " + msg, e);
	}
	
	private void log(PrintStream s, String msg){
		log(s, msg, null);
	}
	
	private void log(PrintStream s, String msg, Exception e){
		// Synchronize to make sure there are no mixed messages
		synchronized(Logger.class){
			s.printf("%s :  %s%n", className, msg);
		
			if(e != null)
				e.printStackTrace(s);
		}
	}
	
	private Logger(Class<?> _class){
		this.className = _class.getSimpleName();
	}
}

